package com.nbsaas.boot.project.ext.resource;

import com.nbsaas.boot.project.api.apis.ProjectApi;
import com.nbsaas.boot.project.api.apis.ProjectCatalogApi;
import com.nbsaas.boot.project.api.domain.field.ProjectField;
import com.nbsaas.boot.project.api.domain.request.ProjectCatalogRequest;
import com.nbsaas.boot.project.ext.apis.ProjectCatalogExtApi;
import com.nbsaas.boot.rest.filter.Filter;
import com.nbsaas.boot.rest.response.ResponseObject;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class ProjectCatalogExtResource implements ProjectCatalogExtApi {

    @Resource
    private ProjectCatalogApi projectCatalogApi;

    @Resource
    private ProjectApi projectApi;


    @Transactional
    @Override
    public ResponseObject<?> deleteExt(ProjectCatalogRequest request) {

        Long count = projectApi.countData(Filter.eq(ProjectField.catalog, request.getId()));
        if (count == null) {
            count = 0L;
        }
        if (count > 0) {
            return ResponseObject.error("该分类下面有项目，请先删除该分类下面的项目");
        }

        return projectCatalogApi.delete(request);
    }
}
